File descriptor

In computer programming, a file descriptor is an abstract indicator for accessing a file. The term is generally used in POSIX operating systems. In Microsoft Windows terminology and in the context of the C standard I/O library, "file handle" is preferred, though the latter case is technically a different object (see below).

In POSIX, a file descriptor is an integer, specifically of the C type int. There are 3 standard POSIX file descriptors which presumably every process (save perhaps a daemon) should expect to have:

Integer value Name
0 Standard Input (stdin)
1 Standard Output (stdout)
2 Standard Error (stderr)

Generally, a file descriptor is an index for an entry in a kernel-resident data structure containing the details of all open files. In POSIX this data structure is called a file descriptor table, and each process has its own file descriptor table. The user application passes the abstract key to the kernel through a system call, and the kernel will access the file on behalf of the application, based on the key. The application itself cannot read or write the file descriptor table directly.

In Unix-like systems, file descriptors can refer to files, directories, block or character devices (also called "special files"), sockets, FIFOs (also called named pipes), or unnamed pipes.

The FILE * file handle in the C standard I/O library routines is technically a pointer to a data structure managed by those library routines; one of those structures usually includes an actual low level file descriptor for the object in question on Unix-like systems. Since file handle refers to this additional layer, it is not interchangeable with file descriptor.

To further complicate terminology, Microsoft Windows also uses the term file handle to refer to the more low-level construct, akin to POSIX's file descriptors. Microsoft's C libraries also provide compatibility functions which "wrap" these native handles to support the POSIX-like convention of integer file descriptors as detailed above.

Contents

Operations on file descriptors

Modern Unix-like systems typically provide the following operations on file descriptors:

Creating file descriptors

Deriving file descriptors

Operations on a single file descriptor

Operations on multiple file descriptors

Operations on the file descriptor table

Operations that modify process state

File locking

Sockets

Miscellaneous

Upcoming operations

A series of new operations on file descriptors has been added to many modern Unix-like systems, as well as numerous C libraries, to be standardized in a future version of POSIX.[1] The at suffix signifies that the function takes an additional first argument supplying a file descriptor from which relative paths are resolved, the forms lacking the at suffix thus becoming equivalent to passing a file descriptor corresponding to the current working directory. The purpose of these new operations is to defend against a certain class of TOCTTOU attacks.

The Native API of the Windows NT family of operating systems also allows callers to specify a root directory when they open a file or other object by name.[2]

File descriptors as capabilities

Unix file descriptors behave in many ways as capabilities. They can be passed between processes across Unix domain sockets using the sendmsg() system call. Note, however, that what is actually passed is a reference to an "open file description" that has mutable state (the file offset, and the file status and access flags). This complicates the secure use of file descriptors as capabilities, since when programs share access to the same open file description, they can interfere with each other's use of it by changing its offset or whether it is blocking or non-blocking, for example.[3][4] In operating systems that are specifically designed as capability systems, there is very rarely any mutable state associated with a capability itself.

A Unix process' file descriptor table is an example of a C-list.

See also

References

  1. ^ Extended API Set, Part 2. The Open Group. October 2006. ISBN 1931624674. http://www.opengroup.org/bookstore/catalog/c063.htm. 
  2. ^ ZwCreateFile in MSDN Library
  3. ^ [1]
  4. ^ Jonathan de Boyne Pollard (2007). "Don't set shared file descriptors to non-blocking I/O mode.". Frequently Given Answers. http://homepage.ntlworld.com./jonathan.deboynepollard/FGA/dont-set-shared-file-descriptors-to-non-blocking-mode.html.